import crypto from '../crypto'

function getItem(name) {
    return localStorage.getItem(name);
}

function getJsonItem(name) {
    return JSON.parse(localStorage.getItem(name));
}

const getters = {
    campaigns: state => {

        if (state.campaigns == null) {
            state.campaigns = getJsonItem('campaigns')
        }

        return state.campaigns
    },
    userInfo: state => {
        let userInfoData = state.user.userInfo;
        if (userInfoData == null) {
            userInfoData = JSON.parse(getItem('userInfo') ? crypto.get(getItem('userInfo'), 'userInfo') : '{}')
        }
        return userInfoData
    },
    userByCompany: state => {
        let userByCompanyData = state.user.userByCompany;
        return userByCompanyData == null ? getJsonItem('userByCompany') : userByCompanyData
    },
    userId: state => {
        let userIdData = state.user.userId;
        if (userIdData == null) {
            userIdData = getJsonItem('userId')
        }
        return userIdData
    },
    companyInfo: state => {
        let companyInfoData = state.user.companyInfo;
        if (companyInfoData == null) {
            companyInfoData = JSON.parse(crypto.get(getItem('companyInfo'), 'companyInfo'))
        }
        return companyInfoData
    },
    iabCategories: state => {
        let iabCategoriesData = state.publicData.iabCategories;
        if (iabCategoriesData == null) {
            iabCategoriesData = getJsonItem('iabCategories')
        }
        return iabCategoriesData
    },
    countries: state => {
        let countriesData = state.publicData.countries;
        if (countriesData == null) {
            countriesData = getJsonItem('countries')
        }
        return countriesData
    },
    campaignList: state => {
        let campaignListData = state.publicData.campaignList;
        if (campaignListData == null) {
            campaignListData = getJsonItem('campaignList')
        }
        return campaignListData
    },
    /***
     *  查询当前登录用户是否有管理员权限
     * @param state
     * @returns {boolean}
     */
    usIsAdmin: state => {
        let userInfoData = state.user.userInfo;
        if (userInfoData == null) {
            userInfoData = JSON.parse(getItem('userInfo') ? crypto.get(getItem('userInfo'), 'userInfo') : '{}')
        }
        return userInfoData.roleId === 1
    },
    /***
     * 更加当前主题返回 需要对应的背景颜色
     * @param state
     * @returns {string}
     */
    switchBackColor: state => {
        let companyInfoData = state.user.companyInfo;
        if (companyInfoData == null) {
            companyInfoData = JSON.parse(crypto.get(getItem('companyInfo')))
        }
        return companyInfoData.setStyle === "white" ? "#333" : "#DBDBDB"
    },

    getServeTF: state => {
        return state.user.isTermsOfUse
    },
    getServePP: state => {
        return state.user.isPrivacyPolicy
    }
}

export default getters
